let BasicAttri = require('./basicAttri');
let FightAttri = require('./fightAttri');
let BasicInfo = require('./basicInfo');
let Package = require('./package');
let mainHandle = require('../core/mainHandle');
let totalConfig = mainHandle.totalConfig;
let playerConfig = totalConfig.player[100000];
let basicAttriConfig = playerConfig.basicAttri;
let skillConfig = totalConfig.skill;

class Player {
    constructor(opts) {
        opts = opts || {}
        this.id = opts.id || 0;
        this.name = opts.name || '';
        this.basicInfo = new BasicInfo(opts.basicInfo) //基础信息
        this.basicAttri = new BasicAttri(basicAttriConfig) //基础属性
        this.fightAttri = new FightAttri() //战斗属性
        this.bag = new Package(opts.bag)
        this.initPlayer();
    }

    //初始化角色信息
    initPlayer() {
        this.calcAttri()
    }

    //计算人物属性
    calcAttri(fightAttri) {
        let basicAttri = this.basicAttri,
            basicInfo = this.basicInfo;
        fightAttri = fightAttri || this.fightAttri;
        fightAttri.attack += basicAttri.attack; //攻击力
        fightAttri.maxHp += basicAttri.maxHp; //生命
        fightAttri.defend += basicAttri.defend; //防御
        fightAttri.lvl = basicInfo.lvl;
        fightAttri.hp = fightAttri.maxHp;
        fightAttri.skills = playerConfig.initSkills.slice() || this.basicInfo.skills;
        fightAttri.skillsCd = fightAttri.skills.map((skillId) => {
            return skillConfig[skillId].cd;
        })
    }

    initFightAttri() {
        let fightAttri = new FightAttri(this.fightAttri),
            basicInfo = this.basicInfo;
        fightAttri.skills = playerConfig.initSkills.slice() || this.basicInfo.skills;
        fightAttri.skillsCd = fightAttri.skills.map((skillId) => {
            return skillConfig[skillId].cd;
        })
        fightAttri.hp = this.fightAttri.maxHp;
        fightAttri.lvl = basicInfo.lvl;
        return fightAttri;
    }

    fightAttriCopy() {
        return new FightAttri(this.fightAttri)
    }

    getExp(exp) {
        this.basicInfo.exp += exp;
        let lvlexp = playerConfig.lvlexp[this.basicInfo.lvl - 1];
        if (this.basicInfo.exp >= lvlexp) {
            this.basicInfo.lvl += 1;
            this.calcAttri()
        }
    }

}

module.exports = Player;